package com.blb.thread;

import java.util.concurrent.locks.ReentrantLock;

public class LockDemo{

    //创建重入锁
    private ReentrantLock lock = new ReentrantLock();

    public void testLock(){
        //上锁
        lock.lock();
        try {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "--->" + i);
            }
        }finally {
            //释放锁
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockDemo lockDemo = new LockDemo();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                lockDemo.testLock();
            }).start();
        }
    }
}
